package com.rzjm.api.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.api.dao.*;
import com.rzjm.api.entity.*;
import com.rzjm.api.exception.TransactionalException;
import com.rzjm.api.service.CkTerminalService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


@Service
public class CkTerminalServiceImpl extends BaseServiceImpl<CkTerminalDao, CkTerminal> implements CkTerminalService {

    @Resource
    private UserDao userDao;
    @Resource
    private SysUserDao sysUserDao;
    @Resource
    private CkTerminalDao ckTerminalDao;
    @Resource
    private TerminalCheckRecordDao terminalCheckRecordDao;
    @Resource
    private TerminalRecordRepairDao terminalRecordRepairDao;
    @Resource
    private TerminalCheckRecordImgDao terminalCheckRecordImgDao;

    @Value("${web.imgprefix}")
    private String imgprefix;//图片地址

    @Override
    public List<CkTerminal> getTerminalList(User user, Integer state,String keyword,  Integer offset, Integer pageSize) {
        Map<String,Object> map  = new HashMap<>();
        SysUser sysUser = sysUserDao.selectById(user.getSysUserId());
        if(sysUser != null){
            if(sysUser.getUserType() >= 4){
                map.put("shop_id",sysUser.getShopId());
            } else if(sysUser.getUserType() == 2){
                map.put("jituan_user_id",sysUser.getShopId());
            } else if(sysUser.getUserType() == 3){
                map.put("sys_user_id",sysUser.getShopId());
            }
        }
        if(state >= 0){
            map.put("state",state);
        }
        if(StrUtil.isNotEmpty(keyword)){
            map.put("keyword",keyword);
        }
        List<CkTerminal> list = ckTerminalDao.getPageByUserId(map,offset,pageSize);
        list.forEach(terminal -> {
            QueryWrapper<TerminalCheckRecord> qw = new QueryWrapper<>();
            qw.eq("terminal_id",terminal.getId());
            qw.eq("check_state",2);
            qw.eq("repair_state",0);
            qw.eq("is_del",0);
            qw.orderByDesc("create_time");
            List<TerminalCheckRecord> records = terminalCheckRecordDao.selectList(qw);
            if(records != null){
                records.forEach(record ->{
                    List<TerminalCheckRecordImg> imgs = terminalCheckRecordImgDao.selectListByColumn("record_id",record.getId());
                    if(imgs!=null){
                        imgs.forEach(img->{
                            img.setImgUrl(imgprefix + img.getImgUrl());
                        });
                    }
                    record.setImgs(imgs);
                });
            }
            terminal.setRecords(records);
        });
        return list;
    }

    /**
     *  维修处理
     * @param repairName
     * @param result_op
     * @param ckTerminal
     * @param record
     * @return
     * @throws TransactionalException
     */
    @Transactional
    @Override
    public int repair(String repairName,String result_op,CkTerminal ckTerminal, TerminalCheckRecord record)throws TransactionalException {
        try{
            ckTerminal.setState(1);
            int ret = ckTerminalDao.updateById(ckTerminal);
            if(ret <= 0){
                throw  new TransactionalException("ckTerminalDao update error");
            }
            record.setRepairState(1);
            ret = terminalCheckRecordDao.updateById(record);
            if(ret <= 0){
                throw  new TransactionalException("ckTerminalRecordRepairDao update error");
            }

            TerminalRecordRepair recordRepair = new TerminalRecordRepair();
            recordRepair.setRecordId(record.getId());
            recordRepair.setTerminalId(record.getTerminalId());
            recordRepair.setRepairTime(new Date());
            recordRepair.setRepairName(repairName);
            recordRepair.setRepairState(record.getRepairState());
            recordRepair.setResult(result_op);
            ret = terminalRecordRepairDao.insert(recordRepair);
            if(ret <= 0){
                throw  new TransactionalException("ckTerminalRecordRepairDao insert error");
            }
            return recordRepair.getId();
        }catch (Exception e){
            e.printStackTrace();
            throw  new TransactionalException("");
        }
    }
}
